local function rows(conn, cmd)
    local cur = conn:execute(cmd)
    return function ()
      return cur:fetch()
    end    
end 

local function main(conn, msg)
    local type = msg['behavior']
    if type == "add" then
        local Nowtime = os.date("%Y-%m-%d %H:%M:%S")
        local sql = string.format("INSERT INTO `challenge`(`last_update_time`, `from_user`, `to_user`) VALUES ('%s', '%s', %s);", Nowtime, msg['from_user'],msg['to_user'])
        local affected_rows = conn:execute(sql)
        return affected_rows > 0
    elseif type == "get" then
        local condition = msg["condition"] or ""
        local sql = "SELECT `last_update_time`, `from_user`, `to_user` FROM `challenge`"
        if condition ~= "" then
            sql = sql..condition
        end
        local data = {}
        for last_update_time,from_user,to_user in rows(conn, sql) do
            local span = {}
            span.last_update_time = last_update_time
            span.from_user = from_user
            span.to_user = to_user
            data[#data+1] = span
        end    
        return data    
    elseif type == "del" then   
        local sql = "DELETE * FROM challenge"
        local condition = msg["condition"] or ""
        if condition ~= "" then
            sql = sql..condition
        end
        local affected_rows = conn:execute(sql)
        return affected_rows > 0 
    end    

end 


return main